
from unisdk.sms import UniSMS
from unisdk.exception import UniException
from django.conf import settings


def send_sms(phone_num, template_id, template_param_list):
    UNI_SMS_ACCESSKEY_ID = settings.UNI_SMS_ACCESSKEY_ID
    UNI_SMS_ACCESSKEY_SECRET = settings.UNI_SMS_ACCESSKEY_SECRET
    # 初始化
    client = UniSMS(UNI_SMS_ACCESSKEY_ID, UNI_SMS_ACCESSKEY_SECRET)  # 若使用简易验签模式仅传入第一个参数即可

    try:
        # 发送短信
        res = client.send({
            "to": phone_num,
            "signature": settings.UNI_SMS_SIGN,
            "templateId": template_id,
            "templateData": {
                "code": template_param_list
            }
        })
        print(res.data)
        response = {'result': 0, 'errmsg': "发送成功"}

    except UniException as e:
        response = {'result': 1000, 'errmsg': "网络异常发送失败"}
        print(e)

    return response